<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑用户</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css"/>
</head>
<body>
<div style="padding-top: 20px;padding-right: 40px">
    <form class="layui-form" action="" lay-filter="user">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required|username"
                       placeholder="请输入登录用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账户</label>
            <div class="layui-input-block">
                <input type="text" name="account" required lay-verify="required|account"
                       placeholder="请输入登录账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登录密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required|pass"
                       placeholder="请输入登录密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="password2" required lay-verify="required"
                       placeholder="请再次输入登录密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="userinfo">创建用户</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/sha.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="lib/layui/layui.js"></script>
<script>
    layui.use(['form','layer'], function () {
        var form = layui.form;
        var layer=layui.layer;
        //监听提交
        form.on('submit(userinfo)', function (data) {
            var field=data.field
            if(field.password!=field.password2){
                layer.msg("两次输入密码不一致")
                return false;
            }
            var userItem={
                name:encodeURIComponent(field.username),
                account:field.account,
                pwd:hex_sha1(field.password)
            }
            saveUser(userItem);
            return false;
        });

        form.verify({
            username: function (value, item) {
                if (value.indexOf("=")>=0||value.indexOf("'")>=0||value.indexOf("\"")>=0) {
                    return '用户名中包含非法字符';
                }
            },
            account: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '账户不能有特殊字符';
                }
                if (value.length < 5) {
                    return '账户至少5位字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '账户首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '账户不能全为数字';
                }
            }
            ,
            pass: function (value, item){
                if (!new RegExp("^[\\S]{8,30}$").test(value)) {
                    return '密码必须8到30位，且不能出现空格';
                }
                if (!new RegExp("^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\\\W_!@#$%^&*`~()-+=]{8,30}$").test(value)) {
                    return '密码必须包含大写字母、小写字母、数字、特殊字符中至少三项组合';
                }
            } ,
            ip: [/^(?:(?:1[0-9][0-9]\.)|(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5])|(?:[1-9][0-9])|(?:[0-9]))$/, 'IP地址格式不正确']
        });
    });

    function saveUser(userItem) {
        httpPost("admin/create", userItem, function (res) {
            if(res.code==0){
                parent.reloadUserList()
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            }else{
                layui.use(['layer'], function () {
                    var layer=layui.layer;
                    layer.msg(res.msg)
                });
            }
        })
    }
</script>
</body>
</html>